package com.lbd.media.controller;

import com.lbd.base.controller.BaseController;
import com.lbd.commons.Result;
import com.lbd.commons.ResultState;
import com.lbd.media.bean.Media;
import com.lbd.media.service.MediaService;
import com.lbd.system.bean.Employee;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Arrays;

/**
 * Created by zzl on 2016/9/21.
 */
@RestController
@RequestMapping("/media")
public class MediaController extends BaseController {
    private final Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private MediaService mediaService;

    /**
     * 按条件分页查询媒体信息
     *
     * @param page  第几页
     * @param rows  一页多少条
     * @param media
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result list(int page, int rows, Media media) {
        Page<Media> mediaPage = mediaService.findByPageConditionJpql(page, rows, media);
        //处理发布时间
        mediaPage.getContent().forEach(each -> {
            if (StringUtils.isNotEmpty(each.getState())) {
                if (each.getState().equals("01")) {//TODO 使用常量或数据字典
                    //发布状态下，发布时间=变更时间
                    each.setPublishTime(each.getModifiedDate());
                }
            } else {
                //默认草稿
                each.setState("02");
            }

        });
        return new Result(ResultState.SUCCESS, "查询成功", mediaPage);
    }

    /**
     * 保存媒体信息
     *
     * @param media
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result save(Media media, HttpSession session) {
        Employee employee = (Employee) session.getAttribute(Employee.SESSION_USER);
        media.setCreateUserId(employee.getId());
        if (StringUtils.isBlank(media.getPreview()) && StringUtils.isNotBlank(media.getContent())) {
            String previem = "";
            String regx = "<.+?>";
            String content = media.getContent().replaceAll(regx, "");
            if (content.length() > 100) {
                previem = content.substring(0, 100);
            } else {
                previem = content;
            }
            media.setPreview(previem);
        }
        mediaService.save(media);
        return new Result(ResultState.SUCCESS, "添加成功", "");
    }

    /**
     * 删除媒体信息
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public Result del(@RequestParam("ids[]") String[] ids) {
        System.out.println(Arrays.toString(ids));
        mediaService.del(ids);
        return new Result(ResultState.SUCCESS, "删除成功", "");
    }

    /**
     * 根据id查询回显要编辑的媒体信息
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public Result toEdit(String id) {
        return new Result(ResultState.SUCCESS, "加载成功", mediaService.findById(id));
    }

    /**
     * 修改媒体信息
     *
     * @param media
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Result update(Media media) {
        mediaService.update(media);
        return new Result(ResultState.SUCCESS, "更新成功");
    }

}
